# -*- coding:utf-8 -*-
import re
import time
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from bs4 import BeautifulSoup


# driver = webdriver.Chrome()
driver = webdriver.PhantomJS()
wait = WebDriverWait(driver, 10)


# 搜索并获取页码总数
def search(keyword):
	driver.get('https://s.taobao.com/')
	try:
		input = wait.until(
			EC.presence_of_element_located((By.CSS_SELECTOR, '#q'))
		)
		submit = wait.until(
			EC.element_to_be_clickable((By.CSS_SELECTOR, '#J_SearchForm button'))
		)
		input.send_keys(keyword)
		submit.click()
		total_page = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '#mainsrp-pager div.total')))
		return total_page.text
	except TimeoutException:
		return search(keyword)


# 翻页
def next_page(page_number):
	try:
		input = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '#mainsrp-pager div.form input')))
		submit = wait.until((EC.element_to_be_clickable((By.CSS_SELECTOR, '#mainsrp-pager div.form span.btn.J_Submit'))))
		input.clear()
		input.send_keys(page_number)
		submit.click()
		# 判断翻页是否成功（文字在当前元素中）
		wait.until(EC.text_to_be_present_in_element((By.CSS_SELECTOR, '#mainsrp-pager li.item.active span'), str(page_number)))
	except TimeoutException:
		next_page(page_number)  # 如果超时则重新执行该方法

# 解析
def get_products():
	wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '#mainsrp-itemlist .items .item')))
	req_doc = driver.page_source
	soup = BeautifulSoup(req_doc, 'lxml')
	items = soup.select('#mainsrp-itemlist .items .item')

	for item in items:
		products_dict = {
			'img': 'https:' + str(item.select('div.pic a img')[0].get('src')),
			'title': item.select('div.row.row-2.title a')[0].get_text(strip=True),
			'price': item.select('.row .price')[0].get_text(strip=True),
			'saled_count': item.select('.row .deal-cnt')[0].get_text(strip=True),
			'shop': item.select('div.shop a.shopname')[0].get_text(strip=True),
			'location': item.select('div.location')[0].get_text(strip=True),
		}
		print products_dict['img']
		print products_dict['title'],products_dict['price'],products_dict['saled_count']
		print products_dict['shop'],products_dict['location']
		print


def main():
	total = search(u'美食')
	total_page = int(re.compile('(\d+)').search(total).group(1))
	for i in range(2, total_page + 1):
		get_products()
		next_page(i)
		time.sleep(5)


if __name__ == '__main__':
	main()
	driver.close()
